/*
 * @Author: szx
 * @Date: 2022-06-02 16:37:09
 * @LastEditTime: 2022-06-02 16:44:37
 * @Description:
 * @FilePath: \leetcode\1000-1099\1022\1022.js
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumRootToLeaf = function (root) {
    let res = 0;
    const dfs = (root, sum) => {
        if (!root) return;
        sum = sum * 2 + root.val;
        if (!root.left && !root.right) {
            res += sum;
            return;
        }
        dfs(root.left, sum);
        dfs(root.right, sum);
    };
    dfs(root, 0);
    return res;
};
